<?php
declare (strict_types=1);

namespace app\common\logic;

use app\common\model\AdminRoleModel;
use app\common\validate\AdminRoleValidate;
use think\exception\ValidateException;

/**=========================================
 * Copyright © 2023-2024, WICMS.CN. All Rights Reserved
 * Project Name: WiCMS
 * Description: 角色逻辑层
 * Author: jason
 * Email: 52527998@QQ.COM
 * Date: 2024/11/4
 * Time: 16:19
 * File: app\common\Logic\AdminRoleLogic.php
 * =========================================
 */
class AdminRoleLogic extends BaseLogic
{


    public function _initialize(): void
    {
        parent::_initialize();
    }

    public static function deleteData(int $id): bool|string
    {
        if (empty($id)) {
            return '参数错误';
        }
        return AdminRoleModel::destroy($id, true);
    }

    public static function getList(array $condition, bool $is_page = true): array
    {
        if ($is_page) {
            $listRows = 10;
            $list     = (new AdminRoleModel)->where(convertModelCondition($condition))->order('id', 'asc')->paginate($listRows);
        } else {
            $list = (new AdminRoleModel)->where(convertModelCondition($condition))->order('id', 'asc')->select();
        }
        return $list->toArray();
    }

    public static function getRoleMenuIds(int $role_id = 0): array
    {
        if (empty($role_id)) return [];
        return (new AdminRoleModel)->where('id', $role_id)->value('allow_auth');
    }

    /**
     * 保存角色数据，若包含id 则更新，否则新增
     *
     * @param array $data
     *
     * @return bool|string
     */
    public static function saveData(array $data): bool|string
    {
        $role_id = $data['id'] ?? 0;
        unset($data['id']);
        $scene = empty($role_id) ? 'add' : 'edit';
        try {
            validate(AdminRoleValidate::class)
                ->scene($scene)
                ->check($data);
            if (empty($role_id)) {
                $result = AdminRoleModel::create($data);
            } else {
                $result = AdminRoleModel::update($data, ['id' => $role_id]);
            }
            return true;
        } catch (ValidateException $e) {
            return $e->getError();
        }
    }

    /**
     * 更新角色权限
     *
     * @param int   $role_id
     * @param array $allow_auths
     *
     * @return bool|string
     */
    public static function setRoleAllowAuth(int $role_id, array $allow_auths = []): bool|string
    {
        if (empty($role_id) || empty($allow_auths)) {
            return '参数错误';
        }
        sort($allow_auths, SORT_NUMERIC);
        $allow_auths = array_unique($allow_auths);
        $result      = AdminRoleModel::update(['allow_auth' => $allow_auths], ['id' => $role_id]);
        return $result ? true : '保存失败';
    }

    /**
     * 更新角色状态
     *
     * @param int $role_id
     * @param int $status
     *
     * @return bool|string
     */
    public static function setRoleStatus(int $role_id, int $status):bool|string
    {
        if (empty($role_id)) {
            return '参数错误';
        }
        $result = AdminRoleModel::update(['status' => $status], ['id' => $role_id]);
        return $result ? true : '保存失败';
    }

}